
# load packages
library(tidyverse)
library(patchwork)
library(scales)
library(kableExtra)

# set ggplot options
theme_set(theme_bw(base_family = "Gill Sans")) 
update_geom_defaults("label", list(family = theme_get()$text$family))
update_geom_defaults("text", list(family = theme_get()$text$family))

# load data
policies <- read_csv("policies.csv") %>%
  filter(!is.na(policy)) %>%
  mutate(policy = factor(policy, levels = c("No Policy", "Encouraged", "Required", "Verified"))) %>%
  # add "s" to "Journal"
  mutate(apsa_journal = paste0(apsa_journal, "s")) %>%
  mutate(minimal = case_when(policy == "No Policy" ~ FALSE, 
                             policy == "Encouraged" ~ FALSE, 
                             policy == "Required" ~ TRUE, 
                             policy == "Verified" ~ TRUE)) %>%
  glimpse()


# plot (all journals)
gg1 <- policies %>%
  group_by(policy, minimal) %>%
  summarize(count = n(), 
            pct_lbl = scales::percent(n()/nrow(policies))) %>%
  ggplot(aes(x = policy, y = count, label = pct_lbl)) + 
  geom_col(color = "grey30", aes(fill = minimal), alpha = 0.5) + 
  scale_fill_brewer(type = "qual", palette = 2, direction = -1) +
  geom_label(size = 3) + 
  #ggrepel::geom_label_repel(nudge_y = -.1, size = 3, direction = "y") + 
  facet_wrap(vars("All Journals")) + 
  labs(x = "Posted Journal Policy", 
       y = "Number of Journals") + 
  theme(legend.position = "none") +
  scale_y_continuous(expand = expansion(mult = c(0, .1)))

# plot (apsa journals)
gg2 <- policies %>%
  filter(apsa_journal == "APSA Journals") %>%
  group_by(policy, apsa_journal, minimal) %>%
  summarize(count = n(), 
            pct_lbl = scales::percent(n()/nrow(.))) %>%
  ggplot(aes(x = policy, y = count, label = pct_lbl)) + 
  geom_col(color = "grey30", aes(fill = minimal), alpha = 0.5) + 
  scale_fill_brewer(type = "qual", palette = 2, direction = -1) +
  geom_label(size = 3) + 
  facet_wrap(vars(apsa_journal)) + 
  labs(x = "Posted Journal Policy", 
       y = "Number of Journals") + 
  theme(legend.position = "none") +
  scale_y_continuous(expand = expansion(mult = c(0, .1)), labels = label_number(accuracy = 1))

policies %>%
  filter(apsa_journal == "APSA Journals") %>%
  arrange(policy) %>%
  kable(format = "markdown")

# plot (non-apsa journals)
gg3 <- policies %>%
  filter(apsa_journal == "Non-APSA Journals") %>% 
  group_by(policy, apsa_journal, minimal) %>%
  summarize(count = n(), 
            pct_lbl = scales::percent(n()/nrow(.))) %>%
  ggplot(aes(x = policy, y = count, label = pct_lbl)) + 
  geom_col(color = "grey30", aes(fill = minimal), alpha = 0.5) + 
  scale_fill_brewer(type = "qual", palette = 2, direction = -1) +
  geom_label(size = 3) + 
  #ggrepel::geom_label_repel(nudge_y = .01, size = 3, direction = "y") + 
  facet_wrap(vars(apsa_journal)) + 
  labs(x = "Posted Journal Policy", 
       y = "Number of Journals")+ 
  theme(legend.position = "none") +
  scale_y_continuous(expand = expansion(mult = c(0, .1)))
  

# patch together
gg1 + gg2 + gg3

design <- "AAAAABCCCCCDDDDD"
gg1 + plot_spacer() + gg2 + gg3 + plot_layout(design = design)

ggsave("fig1-policies.png", height = 2, width = 8, scale = 1.3)


